<template>
	<el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
			label-width="80px">
			<el-form-item label="角色名称" prop="role">
				<el-input v-model="dataForm.role" placeholder="角色名称"></el-input>
			</el-form-item>
			<el-form-item label="描述" prop="desc">
				<el-input v-model="dataForm.desc" placeholder="描述"></el-input>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
	export default {
		data() {
			return {
				visible: false,
				dataForm: {
					id: 0,
					role: '',
					desc: ''
				},
				dataRule: {
					role: [{
						required: true,
						message: '角色名称不能为空',
						trigger: 'blur'
					}],
					desc: [{
						required: true,
						message: '描述不能为空',
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			init(id) {
				this.dataForm.id = id || 0
				this.visible = true
				this.$nextTick(() => {
					this.$refs['dataForm'].resetFields()
					if (this.dataForm.id) {
						this.$http.post('/role/searchRoleInfo', {
							id: this.dataForm.id
						}).then(resp => {
							if (resp.data.code == 200) {
								this.dataForm.role = resp.data.role
								this.dataForm.desc = resp.data.desc
							}
						})
					}
				})
			},
			// 表单提交
			dataFormSubmit() {
				this.$refs['dataForm'].validate((valid) => {
					if (valid) {
						if (this.dataForm.id) {
							this.$http.post('/role/update', this.dataForm).then(resp => {
								if (resp.data.code === 200) {
									this.$message({
										message: '新增成功',
										type: 'success',
										duration: 1200
									})
									this.visible = false
									this.$emit('refreshDataList')
								}
							})
						} else {
							this.$http.post('/role/insert', this.dataForm).then(resp => {
								if (resp.data.code === 200) {
									this.$message({
										message: '新增成功',
										type: 'success',
										duration: 1200
									})
									this.visible = false
									this.$emit('refreshDataList')
								}
							})
						}
						// this.$http({
						// 	url: this.$http.adornUrl(
						// 		`/mall/role/${!this.dataForm.id ? 'save' : 'update'}`),
						// 	method: 'post',
						// 	data: this.$http.adornData({
						// 		'id': this.dataForm.id || undefined,
						// 		'role': this.dataForm.role,
						// 		'desc': this.dataForm.desc,
						// 		'createTime': this.dataForm.createTime,
						// 		'modifyTime': this.dataForm.modifyTime
						// 	})
						// }).then(({
						// 	data
						// }) => {
						// 	if (data && data.code === 0) {
						// 		this.$message({
						// 			message: '操作成功',
						// 			type: 'success',
						// 			duration: 1500,
						// 			onClose: () => {
						// 				this.visible = false
						// 				this.$emit('refreshDataList')
						// 			}
						// 		})
						// 	} else {
						// 		this.$message.error(data.msg)
						// 	}
						// })
					}
				})
			}
		}
	}
</script>